package com.arithmetic;

import java.util.Arrays;

/**
 * @description: 求两数之和
 * @author: Liu
 * @create: 2021-08-18
 **/
public class OneSunOfTwoNumbers {

    public static void main(String[] args) {
        int[] nums = new int[]{8, 7, 11, 15, 1};
        int target = 9;

        OneSunOfTwoNumbers one = new OneSunOfTwoNumbers();
        int[] ints = one.twoSum(nums, target);
        System.out.println(Arrays.toString(ints));
        // 如果使用这种方法打印，打印的是数组的首地址
        System.out.println(ints);
    }

    /**
     * sums 数组中两个数字的和为target，输出这两个数字在数组中的坐标
     * @param sums
     * @param target
     * @return
     */
    public int[] twoSum(int[] sums, int target) {

        int sumsLength = sums.length;
        int[] ints = new int[2];

        for (int i = 0; i < sumsLength; i++) {
            for (int j = 0; j < sumsLength; j++) {
                if (sums[i] + sums[j] == target) {
                    ints[0] = i;
                    ints[1] = j;
                    return ints;
                }
            }
        }
        return new int[0];
    }


}